2
2
.
.
9
9
.
.
6
6
I
I
n
n
t
t
e
e
r
r
f
f
a
a
c
c
e
e
-
-
@
@
Q
Q
u
u
a
a
l
l
i
i
f
f
i
i
e
e
r
r
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
Another way to tell Spring which implementation to inject is to use @Qualifier Annotation.
@Qualifier is used by using
@Service(name = "implementation1") to give a name to the Spring Component
@Autowired @Qualifier("implementation1") to reference that Spring Component where it needs to be injected
@Primary and @Qualifier Annotations can be used at the same time.
If @Autowired Annotation doesn't specify @Qualifier, @Primary Implementation will be used.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller and @RequestMapping. Includes Tomcat Server.
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_autowired_qualifier (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside package controllers)
Create Package: services (inside main package)
Create Interface: MyServiceInterface.java (inside package controllers)
Create Class: MyServiceImplementation1.java (inside package controllers)
Create Class: MyServiceImplementation2.java (inside package controllers)
MyController
@Qualifier("impl1")
http://localhost:8080/Hello
Tomcat
Browser
MyServiceInterface
@Service("impl1")
MyServiceImpl...1
@Service("impl2")
MyServiceImpl...2
MyServiceInterface.java
package com.ivoronline.springboot_autowired_qualifier.services;
public interface MyServiceInterface {
public String sayHello();
}
MyServiceImplementation1.java
package com.ivoronline.springboot_autowired_qualifier.services;
import org.springframework.stereotype.Service;
@Service("impl1")
public class MyServiceImplementation1 implements MyServiceInterface {
public String sayHello() {
return "Hello";
}
}
MyServiceImplementation2.java
package com.ivoronline.springboot_autowired_qualifier.services;
import org.springframework.stereotype.Service;
@Service("impl2")
public class MyServiceImplementation2 implements MyServiceInterface {
public String sayHello() {
return "Hello World";
}
}
MyController.java
package com.ivoronline.springboot_autowired_qualifier.controllers;
import com.ivoronline.springboot_autowired_qualifier.services.MyServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Autowired
@Qualifier("impl1")
MyServiceInterface myService;
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
String Results = myService.sayHello();
return Results;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>